package oj;

public class LC844 {
    public boolean backspaceCompare(String s, String t) {
        int i=s.length()-1;
        int j=t.length()-1;
        int countS=0,countJ=0;
        while(i>=0 || j>=0){
            while(i>=0){
                if(s.charAt(i)=='#'){
                    countS++;
                    i--;
                }else if(countS>0){
                    countS--;
                    i--;
                }else{
                    break;
                }
            }

            while(j>=0){
                if(t.charAt(j)=='#'){
                    countJ++;
                    j--;
                }else if(countJ>0){
                    countJ--;
                    j--;
                }else{
                    break;
                }
            }

            if(i>=0 && j>=0){
                if(s.charAt(i)!=t.charAt(j)){
                    return false;
                }
            }else {
                if(i>=0 || j>=0) return false;

            }
            i--;
            j--;

        }
        return true;
    }
}
